import { Component, OnInit } from '@angular/core';
import { FormGroup, FormBuilder, Validators } from '@angular/forms';
import { RouterModule, Routes, Router, ActivatedRoute, ParamMap } from '@angular/router';
import { MdlDialogService, MdlDialogReference, MdlSnackbarService } from '@angular-mdl/core';
import { ConfirmationService, SelectItem } from 'primeng/primeng';
import { HumanService } from './service/human.service';
import { flyIn } from '../../common/animation/common.animation';
import { switchMap } from 'rxjs/operators';
@Component({
  templateUrl: './human-list.component.html',
  styleUrls: ['./human-list.component.scss'],
  providers: [HumanService],
  animations: [flyIn]
})
export class HumanListComponent implements OnInit {
  unitId: number;
  display: boolean = false;
  allItems: any[];
  showItems: any[];
  selectedItem: any;
  searchItem: any = {};
  searchForm: FormGroup;

  constructor(
    private humanService: HumanService,
    private router: Router,
    private route: ActivatedRoute,
    private fb: FormBuilder,
    private snackbarService: MdlSnackbarService,
    private confirmationService: ConfirmationService) { }

  ngOnInit() {
    this.route.paramMap.pipe(
      switchMap((params: ParamMap) => {
        this.unitId = +params.get('id');
        return this.humanService.loadSimpleHumansByUnitId(+this.unitId);
      })
    ).subscribe(data => {
      data.forEach((human, i) => human['seq'] = (i + 1));
      this.allItems = this.showItems = data;
      this.humanSelect(data[0]);
    }, error => console.error(error));
    this.buildForm();
  }

  /** 岗位选择事件 */
  humanSelect(human: any) {
    this.showItems.forEach(h => {
      if (human.humanId == h.humanId) {
        h.checkStyle = { 'background-color': 'rgb(3,169,244)' }
        this.selectedItem = h;
      } else {
        h.checkStyle = { 'background-color': 'whitesmoke' }
      }
    });
  }

  /** 修改 */
  update($event) {
    if (this.selectedItem) {
      this.router.navigate(['system/human/info', { "id": this.selectedItem.humanId }]);
    }
  }

  /** 删除 */
  delete($event) {
    if (this.selectedItem) {
      this.confirmationService.confirm({
        header: '确认',
        message: '您确定要删除选择的记录吗？',
        accept: () => {
          this.humanService.delete(this.selectedItem.humanId).subscribe(data => {
            this.snackbarService.showToast("数据删除成功！");
            this.showItems.forEach((r, index) => {
              if (this.selectedItem.humanId == r.humanId)
                this.showItems.splice(index, 1);
            });
            this.selectedItem = null;
          })
        }
      });
    }
  }
  /** 新增 */
  add($event) {
    this.router.navigate(['/system/human/info', { 'unitId': this.unitId }]);
  }
  /**查询 */
  search($event) {
    this.searchItem = this.searchForm.value;
    this.showItems = [];
    this.allItems.forEach(item => {
      if (item.humanName.indexOf(this.searchItem.humanName) != -1
        && item.humanId + "".indexOf(this.searchItem.humanId + "") != -1) {
        this.showItems.push(item);
      }
    });
  }
  buildForm(): void {
    this.searchForm = this.fb.group({
      'humanId': [this.searchItem.humanId],
      'humanName': [this.searchItem.humanName]
    });
  }
}
